package com.jacky.concurrent;

import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by jacky on 2018/2/12.
 */
public class MyLockDemo {
    private final static MyLock lock = new MyLock(false);
    //private final static ReentrantLock lock = new ReentrantLock(true);
    private static int num = 0;
    public static void main(String[] args) {
        int count = 2000;
        CountDownLatch countDownLatch = new CountDownLatch(count);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                Thread thread = Thread.currentThread();
                System.out.println("--start--"+thread.getName());
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num++;
                System.out.println("--end--");
                countDownLatch.countDown();
                }finally {
                   lock.unlock();
                }

            }
        };
        Thread thread = null;
        for (int i = 0; i < count; i++) {
            thread = new Thread(runnable,"t"+i);
            thread.start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("--num--"+num);
    }
}
